package com.googlecode.worktools.ui;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.JButton;

import javax.swing.WindowConstants;
import javax.swing.border.LineBorder;
import javax.swing.border.TitledBorder;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import com.googlecode.worktools.service.pwd.PWD;
import com.googlecode.worktools.utils.UIUtils;
import com.googlecode.worktools.utils.Utils;

/**
 * 加密解密功能主面板
 * @author Kobe Su,2009-12-1
 */
public class PWDPanel extends BaseFunctionPanel {
	private JPanel jPanel1;
	private JButton jButton1;
	private JButton jButton2;
	private JTextField jTextField4;
	private JLabel jLabel4;
	private JTextField jTextField3;
	private JLabel jLabel3;
	private JPanel jPanel3;
	private JTextField jTextField2;
	private JLabel jLabel2;
	private JTextField jTextField1;
	private JLabel jLabel1;
	private PWD pwd;
	/**
	* Auto-generated main method to display this 
	* JPanel inside a new JFrame.
	*/
	public static void main(String[] args) {
		JFrame.setDefaultLookAndFeelDecorated(true);
		JFrame frame = new JFrame();
		frame.getContentPane().add(new PWDPanel());
		frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
		frame.pack();
		frame.setLocationRelativeTo(null);
		frame.setVisible(true);
	}
	
	public PWDPanel() {
		super();
		initGUI();
	}
	
	private void initGUI() {
		try {
			this.setPreferredSize(new java.awt.Dimension(568, 350));
			this.setLayout(null);
			{
				jPanel1 = new JPanel();
				this.add(jPanel1);
				jPanel1.setBounds(27, 12, 529, 122);
				jPanel1.setLayout(null);
				jPanel1.setBorder(BorderFactory.createTitledBorder(new LineBorder(new java.awt.Color(0,0,0), 2, false), "\u89e3\u5bc6", TitledBorder.LEADING, TitledBorder.TOP, new java.awt.Font("Dialog",1,12), new java.awt.Color(255,0,0)));
				{
					jLabel1 = new JLabel();
					jPanel1.add(jLabel1);
					jLabel1.setText("\u5bc6\u7801");
					jLabel1.setBounds(38, 25, 26, 18);
				}
				{
					jTextField1 = new JTextField();
					jPanel1.add(jTextField1);
					jTextField1.setBounds(76, 23, 418, 22);
				}
				{
					jLabel2 = new JLabel();
					jPanel1.add(jLabel2);
					jLabel2.setText("\u660e\u6587");
					jLabel2.setBounds(38, 53, 26, 18);
				}
				{
					jTextField2 = new JTextField();
					jPanel1.add(jTextField2);
					jTextField2.setBounds(76, 51, 418, 22);
				}
				{
					jButton1 = new JButton();
					jPanel1.add(jButton1);
					jButton1.setText("\u89e3\u5bc6");
					jButton1.setBounds(403, 77, 60, 28);
					jButton1.addActionListener(new ActionListener() {
						public void actionPerformed(ActionEvent evt) {
							jButton1ActionPerformed(evt);
						}
					});
				}
			}
			{
				jPanel3 = new JPanel();
				this.add(jPanel3);
				jPanel3.setBorder(BorderFactory.createTitledBorder(new LineBorder(new java.awt.Color(0,0,0), 2, false), "\u52a0\u5bc6", TitledBorder.LEADING, TitledBorder.TOP, new java.awt.Font("Dialog",1,12), new java.awt.Color(255,0,0)));
				jPanel3.setLayout(null);
				jPanel3.setBounds(27, 150, 529, 122);
				{
					jLabel3 = new JLabel();
					jPanel3.add(jLabel3);
					jLabel3.setText("\u660e\u6587");
					jLabel3.setBounds(38, 25, 26, 18);
				}
				{
					jTextField3 = new JTextField();
					jPanel3.add(jTextField3);
					jTextField3.setBounds(76, 23, 418, 22);
				}
				{
					jLabel4 = new JLabel();
					jPanel3.add(jLabel4);
					jLabel4.setText("\u5bc6\u7801");
					jLabel4.setBounds(38, 53, 26, 18);
				}
				{
					jTextField4 = new JTextField();
					jPanel3.add(jTextField4);
					jTextField4.setBounds(76, 51, 418, 22);
				}
				{
					jButton2 = new JButton();
					jPanel3.add(jButton2);
					jButton2.setText("\u52a0\u5bc6");
					jButton2.setBounds(405, 77, 60, 28);
					jButton2.addActionListener(new ActionListener() {
						public void actionPerformed(ActionEvent evt) {
							jButton2ActionPerformed(evt);
						}
					});
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private void jButton1ActionPerformed(ActionEvent evt) {
		if(pwd == null)
			pwd = new PWD();
		String pwdStr = jTextField1.getText();
		jTextField2.setText(pwd.decode(pwdStr));
	}

	private void jButton2ActionPerformed(ActionEvent evt) {
		
		String pwdStr = jTextField3.getText();
		if(Utils.getRealLength(pwdStr) > 30) {
			UIUtils.showErrorMsg(PWDPanel.this, "明文字符长度不能超过30");
			return;
		}
			
		PWD	_pwd = new PWD(pwdStr);
		jTextField4.setText(_pwd.encode());
	}
	
	
	
	
	
	
	
	
	
	
	
}
